package utils;

import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.aton.db.Config;

/**
 * redis 链接池
 * @author wangshaoming
 *
 */
public class RedisUtilbak {
	protected static ReentrantLock lockPool=new ReentrantLock();
	protected static ReentrantLock lockJedis=new ReentrantLock();
	public static final Logger log=LoggerFactory.getLogger("RedisPool");
	private static int PORT=Integer.parseInt(Config.getProperty("jedis.port"));
	private static String URL=Config.getProperty("jedis.url");
	private static String PASSWORD=Config.getProperty("jedis.password");
	private static int MAX_ACTIVE=Integer.parseInt(Config.getProperty("jedis.max_active"));
	private static int MAX_IDE=Integer.parseInt(Config.getProperty("jedis.max_ide"));
	private static int MAX_WAIT=Integer.parseInt(Config.getProperty("jedis.max_wait"));
	private static int TIMEOUT=Integer.parseInt(Config.getProperty("jedis.timeout"));
	private static Boolean TEST_ON_BORROW=Boolean.parseBoolean(Config.getProperty("jedis.test_on_borrow"));
	private static Boolean TEST_ON_RETURN=Boolean.parseBoolean(Config.getProperty("jedis.test_on_return"));
	private static JedisPool jedisPool=null;
	/**
	 * 初始化Redis连接池
	 */
	private static void initalPool(){
		try{
			JedisPoolConfig config=new JedisPoolConfig();
			config.setMaxActive(MAX_ACTIVE);
			config.setMaxIdle(MAX_IDE);
			config.setMaxWait(MAX_WAIT);
			config.setTestOnBorrow(TEST_ON_BORROW);
//			config.setTestOnReturn(TEST_ON_RETURN);
			if(StringUtils.isEmpty(PASSWORD)){
				jedisPool=new JedisPool(config,URL,PORT,TIMEOUT);
			}
			else{
				jedisPool=new JedisPool(config,URL,PORT,TIMEOUT,PASSWORD);
			}
			
		}catch(Exception e){
			log.info("------------------初始化连接池异常----------------------");
			e.printStackTrace();
		}
	}
	/**
	 * 多线程下同步初始化
	 */
	private static void poolInit(){
		lockPool.lock();
		try{
			if(jedisPool==null){
				initalPool();
			}
		}catch(Exception e){
			log.info("------------------多线程初始化连接池异常----------------------");
			e.printStackTrace();
		}
		finally{
			lockPool.unlock();
		}
	}
	/**
	 * 获取redis实例
	 */
	public static Jedis getJedis(){
		lockJedis.lock();
		if(jedisPool==null){
			poolInit();
		}
		Jedis jedis=null;
		try{
			if(jedisPool!=null){
				jedis=jedisPool.getResource();
			}
		}catch(Exception e){
			e.printStackTrace();
			log.info("------------------获取redis连接异常----------------------");
		}
		finally{
			returnResource(jedis);
			lockJedis.unlock();
		}
		return jedis;
	}
	/**
	 * 释放redis资源
	 * @param jedis
	 */
	public static void returnResource(final Jedis jedis){
		if(jedis!=null&&jedisPool!=null){
			jedisPool.returnResource(jedis);
		}
	}
	/**
	 * 设置redis的值
	 * @param key
	 * @param value
	 */
	public synchronized static void setString(String key,String value){
		try{
			value=StringUtils.isEmpty(value)?"":value;
			getJedis().set(key,value);
		}catch(Exception e){
			e.printStackTrace();
			log.info("------------------设置redis值异常----------------------");
		}
	}
	/**
	 * 设置过期时间
	 * @param key
	 * @param seconds
	 * @param value
	 */
	public synchronized static void setString(String key,int seconds,String value){
		try{
			value=StringUtils.isEmpty(value)?"":value;
			getJedis().setex(key, seconds, value);
		}catch(Exception e){
			e.printStackTrace();
			log.info("------------------设置redis过期值异常----------------------");
		}
	}
	
	/**
	 * 获取String 值
	 * @param key
	 * @return
	 */
	public synchronized static String getString(String key){
		if(getJedis()==null||!getJedis().exists(key)){
			return null;
		}
		return getJedis().get(key);
	}
}
